import UnitSelect from '@/components/unit/UnitSelect.vue'
import { approveOptions } from '@/plugins/approve'
import { concatEndTime, concatStartTime, shortcuts } from '@/plugins/date'
import { IFormSchemas, ITableColumns } from '@/type/table'
import { h } from 'vue'
import { ElLink } from 'element-plus'
import ApproveShow from '@/components/statusShow/approve/ApproveShow.vue'
import MatterTodoSelect from '@/components/statusShow/matterTodo/MatterTodoSelect.vue'
import MatterTodoShow from '@/components/statusShow/matterTodo/MatterTodoShow.vue'

// 申请列表项
export function generateTableColumn(
  callback?: (columnType: string, row: Record<string, any>) => void,
  approve = false,
) {
  const tableColumns: ITableColumns[] = [
    {
      label: 'ID',
      prop: 'id',
      width: 80,
      render: (row) => {
        return h(
          ElLink,
          {
            type: 'primary',
            underline: 'never',
            onClick: () => callback('id', row),
          },
          () => row.id,
        )
      },
    },
    {
      label: '关联事项标题',
      prop: 'matterTitle',
      minWidth: 120,
    },
    {
      label: '延期天数',
      prop: 'delayDay',
      minWidth: 80,
    },
    {
      label: '原期限',
      prop: 'oldDeadline',
      minWidth: 120,
    },
    approve
      ? {
          label: '办理状态',
          prop: 'applyStatus',
          width: 100,
          render: (row) => {
            return h(MatterTodoShow, {
              status: row.applyStatus,
            })
          },
        }
      : {
          label: '审批状态',
          prop: 'applyStatus',
          width: 100,
          render: (row) => {
            return h(ApproveShow, {
              status: row.applyStatus,
            })
          },
        },
    {
      label: '审批人',
      prop: 'approveUserName',
      minWidth: 100,
    },
    {
      label: '申请单位',
      prop: 'applyUnitName',
      minWidth: 120,
    },
    {
      label: '申请人',
      prop: 'applyUserName',
      minWidth: 80,
    },
    {
      label: '申请时间',
      prop: 'applyTime',
      width: 200,
    },
  ]
  return tableColumns
}

// 处理请求参数
export function dealSearchParams(params: any) {
  return {
    ...params,
    applyStartTime: concatStartTime(params?.applyTime?.[0]),
    applyEndTime: concatEndTime(params?.applyTime?.[1]),
  }
}

const beforeFormSchema: IFormSchemas[] = [
  {
    label: '关联事项标题',
    prop: 'matterTitle',
    type: 'input',
    placeholder: '请输入事项标题',
    span: 6,
    componentProps: {
      maxlength: 50,
      clearable: true,
    },
  },
  {
    label: '申请单位',
    prop: 'applyUnitId',
    type: (props) => {
      return h(UnitSelect, {
        ...props,
        clearable: true,
        placeholder: '请选择申请单位',
      })
    },
    span: 6,
  },
  {
    label: '申请人',
    prop: 'applyUserName',
    type: 'input',
    placeholder: '请输入申请人',
    span: 6,
    componentProps: {
      maxlength: 50,
      clearable: true,
    },
  },
]

const afterSearchSchame: IFormSchemas[] = [
  {
    label: '申请时间',
    prop: 'applyTime',
    type: 'date-picker',
    span: 8,
    componentProps: {
      type: 'daterange',
      'range-separator': '到',
      'start-placeholder': '开始时间',
      'end-placeholder': '结束时间',
      'unlink-panels': true,
      editable: false,
      format: 'YYYY年MM月DD日',
      valueFormat: 'YYYY-MM-DD',
      shortcuts: shortcuts,
      clearable: true,
    },
  },
]

// 申请列表查询参数
export const searchSchame: IFormSchemas[] = [
  ...beforeFormSchema,
  {
    label: '审批状态',
    prop: 'approveResult',
    type: 'select',
    placeholder: '请选择审批状态',
    span: 6,
    componentProps: {
      options: approveOptions,
      clearable: true,
    },
  },
  ...afterSearchSchame,
]

// 审核列表查询参数
export const approveSearchSchame: IFormSchemas[] = [
  ...beforeFormSchema,
  {
    label: '办理状态',
    prop: 'status',
    placeholder: '请选择办理状态',
    span: 6,
    type: (props) => {
      return h(MatterTodoSelect, {
        ...props,
        clearable: true,
      })
    },
  },
  ...afterSearchSchame,
]
